package sql;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

import arbre.utils.ArbreBinaire;
import arbre.utils.Chemin;
import arbre.utils.Polyedre;

public class Serializor {

	public static String sauvegardeFile = "sauvegarde.data";
	public static void sauvegarder(List<Polyedre> polyedres, List<Chemin> chemins, Map<String, ArrayList<String> > intersects){
		try{
			FileOutputStream f_out = new FileOutputStream(sauvegardeFile);

			// Write object with ObjectOutputStream
			ObjectOutputStream obj_out = new ObjectOutputStream (f_out);

			// Write object out to disk
			obj_out.writeObject ( polyedres );
			obj_out.writeObject ( chemins );
			obj_out.writeObject(intersects);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	@SuppressWarnings("unchecked")
	public static void charger(List<Polyedre> polyedres, List<Chemin> chemins, Map<String, ArrayList<String> > intersects){
		try{
			FileInputStream f_in = new FileInputStream(sauvegardeFile);

			// Write object with ObjectOutputStream
			ObjectInputStream obj_in = new ObjectInputStream (f_in);

			// Write object out to disk
			List<Polyedre> polyedres2 = (List<Polyedre>)obj_in.readObject();
			polyedres.addAll(polyedres2);
			
			List<Chemin> chemins2 = (List<Chemin>)obj_in.readObject();
			chemins.addAll(chemins2);
			
			Map<String,ArrayList<String> > intersects2 = (Map<String,ArrayList<String> >) obj_in.readObject();
			intersects.putAll(intersects2);
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	public static void sauvegarderArbre(String filename, ArbreBinaire ar){
		try{
			FileOutputStream f_out = new FileOutputStream(filename);

			// Write object with ObjectOutputStream
			ObjectOutputStream obj_out = new ObjectOutputStream (f_out);

			// Write object out to disk
			obj_out.writeObject ( ar );

		}catch(Exception e){
			e.printStackTrace();
		}
		System.out.println(filename +" saved.");
	}

	public static void chargerArbre(String filename, ArbreBinaire ar){
		try{
			FileInputStream f_in = new FileInputStream(filename);

			// Write object with ObjectOutputStream
			ObjectInputStream obj_in = new ObjectInputStream (f_in);
			
			// Write object out to disk
			ar.setRacine(((ArbreBinaire)obj_in.readObject()).getRacine());
			
		}catch(Exception e){
			e.printStackTrace();
		}
		System.out.println(filename+" loaded.");
	}
	
	public static void saveTreeJSON(String filename, ArbreBinaire ar){
		ObjectMapper mapper= new ObjectMapper();
		try{
			mapper.writeValue(new File(filename), ar);
		}catch (JsonGenerationException e) {
			 
			e.printStackTrace();
	 
		} catch (JsonMappingException e) {
	 
			e.printStackTrace();
	 
		} catch (IOException e) {
	 
			e.printStackTrace();
	 
		}
	}
	
	public static void loadTreeJSON(String filename, ArbreBinaire ar){
		ObjectMapper mapper= new ObjectMapper();
		try{
			ArbreBinaire a = mapper.readValue(new File(filename), ArbreBinaire.class);
			ar.setRacine(a.getRacine());
		}catch (JsonGenerationException e) {
			 
			e.printStackTrace();
	 
		} catch (JsonMappingException e) {
	 
			e.printStackTrace();
	 
		} catch (IOException e) {
	 
			e.printStackTrace();
	 
		}
	}
	
}
